package com.example.login.controller;

import com.example.login.entity.User;
import com.example.login.entity.UserCourse;
import com.example.login.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping(value = "user")
@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    public Map<String, Object> deleteUser(@RequestParam("userName") String userNAme) {
        HashMap response = new HashMap();
        boolean isSuccess = userService.deleteUser(userNAme);
        if (isSuccess) {
            response.put("status", 200);
            response.put("message", "删除成功");
        } else {
            response.put("status", 400);
            response.put("message", "删除失败");
        }
        return response;
    }

    @ResponseBody
    @RequestMapping(value = "/editUser", method = RequestMethod.POST)
    public Map<String, Object> editUser(@RequestParam("userName") String userName, @RequestParam("name") String name, @RequestParam("major") String major, @RequestParam("className") String className, @RequestParam("password") String password) {
        HashMap response = new HashMap();
        boolean isSuccess = userService.editUser(userName, name, major, className, password);
        if (isSuccess) {
            response.put("status", 200);
            response.put("message", "编辑成功");
        } else {
            response.put("status", 400);
            response.put("message", "编辑失败");
        }
        return response;
    }

    @ResponseBody
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
    public Map<String, Object> getUserInfo(@RequestParam(value = "name", required = false) String name,
                                           @RequestParam(value = "major", required = false) String major,
                                           @RequestParam(value = "class_name", required = false) String class_name,
                                           @RequestParam(value = "studentNum", required = false) String studentNum) {
        List<User> userInfoList = userService.getUserInfo(name, major, class_name, studentNum);
        HashMap response = new HashMap();
        for (User userInfo : userInfoList) {
            userInfo.setPassword("******");
        }
        response.put("data", userInfoList);
        return response;
    }

    @ResponseBody
    @RequestMapping(value = "/getStudentScore", method = RequestMethod.GET)
    public Map<String, Object> getUserCourseByUserNameOrStudentNumAndSemester(@RequestParam("userName") String userName,  @RequestParam("semester") String semester) {
        List<UserCourse> userCourseList = userService.getStudentScore(userName, semester);
        HashMap response = new HashMap();
        if (userCourseList != null && !userCourseList.isEmpty()) {
            response.put("status", 200);
            response.put("message", "查询成功");
            response.put("data", userCourseList);
        } else {
            response.put("status", 400);
            response.put("message", "查询失败");
        }
        return response;
    }

    @ResponseBody
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public Map<String, Object> logout(HttpServletRequest request) {
        HashMap<String, Object> response = new HashMap<>();
        request.getSession().removeAttribute("user");
        response.put("code", 200);
        response.put("message", "退出登录成功");
        return response;
    }
}
